/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary 
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */
package com.pekall.todo.util;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.pekall.todo.model.ToDoTasks;

public class SQLiteOperate {
	
	private static final int ID_INDEX = 0;
	private static final int TITLE_INDEX = 1;
	private static final int STATE_INDEX = 2;
	private static final int ALARM_INDEX = 3;
	private static final int BEGIN_INDEX = 4;
	private static final int OVER_INDEX = 5;
	private static final int PRIORITY_INDEX = 6;
	private static final int NOTES_INDEX = 7;
	private static final int REMINDER_INDEX = 8;
	private static final int CREATED_INDEX = 9;
	private static final int MODIFIED_INDEX = 10;
	private static final int TYPE_INDEX = 11;
	public static final String[] sortWay = {" type"," title"," modified"};//排序方式
	
	private Context mContext;
	public SQLiteOperate(Context context){
		mContext = context;
	}
	
	public List<ToDoTasks> loadAll(int which) {
		Cursor cur =  mContext.getContentResolver().query(ToDoSQLHelper.CONTENT_URI, null, null, null, sortWay[which]);
		List<ToDoTasks> toDoTaskses = new ArrayList<ToDoTasks>();
		cur.moveToFirst();
		while (!cur.isAfterLast()) {
			ToDoTasks toDoTasks = new ToDoTasks();
			toDoTasks.setId(cur.getInt(ID_INDEX));
			toDoTasks.setTitle(cur.getString(TITLE_INDEX));
			toDoTasks.setState(cur.getInt(STATE_INDEX));
			toDoTasks.setAlarm(cur.getLong(ALARM_INDEX));
			toDoTasks.setBegin(cur.getLong(BEGIN_INDEX));
			toDoTasks.setOver(cur.getLong(OVER_INDEX));
			toDoTasks.setCreated(cur.getLong(CREATED_INDEX));
			toDoTasks.setModified(cur.getLong(MODIFIED_INDEX));
			toDoTasks.setNotes(cur.getString(NOTES_INDEX));
			toDoTasks.setPriority(cur.getLong(PRIORITY_INDEX));
			toDoTasks.setReminder(cur.getLong(REMINDER_INDEX));
			toDoTasks.setType(cur.getInt(TYPE_INDEX));
			toDoTaskses.add(toDoTasks);
			cur.moveToNext();
		}
		cur.close();
		return toDoTaskses;
	}
	public void deleteById(int id){
		mContext.getContentResolver().delete(ToDoSQLHelper.CONTENT_URI, "_id="+id, null);
	}
	public void saveOrUpdate(ToDoTasks toDoTasks){
		ContentValues  values = new ContentValues();
		
		values.put(ToDoSQLHelper.TITLE, toDoTasks.getTitle());
		values.put(ToDoSQLHelper.STATE, toDoTasks.getState());
		values.put(ToDoSQLHelper.ALARM, toDoTasks.getAlarm());
		values.put(ToDoSQLHelper.BEGIN, toDoTasks.getBegin());
		values.put(ToDoSQLHelper.OVER, toDoTasks.getOver());
		values.put(ToDoSQLHelper.CREATED, toDoTasks.getCreated());
		values.put(ToDoSQLHelper.MODIFIED, toDoTasks.getModified());
		values.put(ToDoSQLHelper.TYPE, toDoTasks.getType());
		values.put(ToDoSQLHelper.NOTES, toDoTasks.getNotes());
		values.put(ToDoSQLHelper.PRIORITY, toDoTasks.getPriority());
		values.put(ToDoSQLHelper.REMINDER, toDoTasks.getReminder());
		if(toDoTasks.getId()==0){
		     mContext.getContentResolver().insert(ToDoSQLHelper.CONTENT_URI, values);
		}
		else
		{
			values.put(ToDoSQLHelper.SID, toDoTasks.getId());
			mContext.getContentResolver().update(ToDoSQLHelper.CONTENT_URI, values, "_id="+toDoTasks.getId(), null);
		}
	}

}
